package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Course;
import cn.sunlelin.educationserver.service.CourseService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.color.CMMException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CourseController {
    @Autowired
    CourseService courseService;

    //Post请求   http://localhost:8089/course/createCourse
    @PostMapping("/course/createCourse")
    public Map<String, Object> createCourse(@RequestBody Course course) {
        courseService.createCourse(course);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "创建课程成功");
        map.put("dataobject", null);
        return map;
    }

    @GetMapping("/course/findAll")
    public Map<String, Object> findAll() {
        List<Map<String, Object>> courseList = courseService.findAll();
        Map<String, Object> map = new HashMap<>();
        if (courseList != null) {
            map.put("code", 200);
            map.put("msg", "查询所有课程成功");
            map.put("dataobject", courseList);
        } else {
            map.put("code", 500);
            map.put("msg", "查询所有课程失败");
            map.put("dataobject", null);
        }
        return map;
    }

    //Get请求   http://localhost:8089/course/selectCourseById/{courseId}
    @GetMapping("/course/selectCourseById/{courseId}")
    public Map<String, Object> selectCourseById(@PathVariable Integer courseId) {
        Map<String, Object> course = courseService.selectCourseById(courseId);
        Map<String, Object> map = new HashMap<>();
        if (course != null) {
            map.put("code", 200);
            map.put("msg", "根据Id查找课程成功");
            map.put("dataobject", course);
        } else {
            map.put("code", 500);
            map.put("msg", "根据Id查找课程失败");
            map.put("dataobject", null);
        }
        return map;
    }

    @GetMapping("/course/selectCourseByCoursetypeId/{coursetypeId}")
    public Map<String, Object> selectCourseByCoursetypeId(@PathVariable Integer coursetypeId) {
        List<Map<String, Object>> courseList = courseService.selectCourseByCoursetypeId(coursetypeId);
        Map<String, Object> map = new HashMap<>();
        if (courseList.size() != 0) {
            map.put("code", 200);
            map.put("msg", "根据课程分类Id查找课程成功");
            map.put("dataobject", courseList);
        } else {
            map.put("code", 500);
            map.put("msg", "根据课程分类Id查找课程失败");
            map.put("dataobject", null);
        }
        return map;
    }


    @GetMapping("/course/selectCourseByFathertypeId/{fathertypeId}")
    public Map<String, Object> selectCourseByFathertypeId(@PathVariable Integer fathertypeId) {
        List<Map<String, Object>> courseList = courseService.selectCourseByFathertypeId(fathertypeId);
        Map<String, Object> map = new HashMap<>();
        if (courseList.size() != 0) {
            map.put("code", 200);
            map.put("msg", "根据大类Id查找课程成功");
            map.put("dataobject", courseList);
        } else {
            map.put("code", 500);
            map.put("msg", "根据大类Id查找课程失败");
            map.put("dataobject", null);
        }
        return map;
    }

    @GetMapping("/course/getPageSearch/{currentPage}/{pageSize}/{input}/{sortModel}")
    public Map<String, Object> getPageSearch(@PathVariable Integer currentPage,
                                             @PathVariable Integer pageSize,
                                             @PathVariable String input,
                                             @PathVariable Integer sortModel) {
        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 2;
        Page<Map<String, Object>> page = courseService.getPageSearch(currentPage, pageSize, input, sortModel);
        List<Map<String, Object>> pageList = page.getResult();
        long total = page.getTotal();
        Map<String, Object> m = new HashMap<>();
        m.put("pageList", pageList);
        m.put("total", total);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "分页查询课程成功");
        map.put("dataobject", m);
        return map;
    }

    @GetMapping("/course/getPageCourse/{currentPage}/{pageSize}")
    public Map<String, Object> getPageCourse(@PathVariable Integer currentPage,
                                             @PathVariable Integer pageSize) {
        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 2;
        Page<Map<String, Object>> page = courseService.getPageCourse(currentPage, pageSize);
        List<Map<String, Object>> pageList = page.getResult();
        long total = page.getTotal();
        Map<String, Object> m = new HashMap<>();
        m.put("pageList", pageList);
        m.put("total", total);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "分页查询全部课程成功");
        map.put("dataobject", m);
        return map;
    }

    @GetMapping("/course/getPageByFathertypeId/{currentPage}/{pageSize}/{fathertypeId}")
    public Map<String, Object> getPageByFathertypeId(@PathVariable Integer currentPage,
                                                     @PathVariable Integer pageSize,
                                                     @PathVariable Integer fathertypeId) {
        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 2;
        Page<Map<String, Object>> page = courseService.getPageByFathertypeId(currentPage, pageSize, fathertypeId);
        List<Map<String, Object>> pageList = page.getResult();
        long total = page.getTotal();
        Map<String, Object> m = new HashMap<>();
        m.put("pageList", pageList);
        m.put("total", total);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "课程大类Id分页查询课程成功");
        map.put("dataobject", m);
        return map;
    }

    @GetMapping("/course/getPageByCoursetypeId/{currentPage}/{pageSize}/{coursetypeId}")
    public Map<String, Object> getPageByCoursetypeId(@PathVariable Integer currentPage,
                                                     @PathVariable Integer pageSize,
                                                     @PathVariable Integer coursetypeId) {
        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 2;
        Page<Map<String, Object>> page = courseService.getPageByCoursetypeId(currentPage, pageSize, coursetypeId);
        List<Map<String, Object>> pageList = page.getResult();
        long total = page.getTotal();
        Map<String, Object> m = new HashMap<>();
        m.put("pageList", pageList);
        m.put("total", total);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "课程类型Id分页查询课程成功");
        map.put("dataobject", m);
        return map;
    }

    @GetMapping("/user/course/getPageByUserId/{currentPage}/{pageSize}")
    public Map<String, Object> getPageByUserId(@PathVariable Integer currentPage,
                                               @PathVariable Integer pageSize,
                                               @RequestHeader("userId") Integer userId) {

        if (currentPage == null) currentPage = 1;
        if (pageSize == null) pageSize = 2;
        Page<Map<String, Object>> page = courseService.getPageByUserId(currentPage, pageSize, userId);
        List<Map<String, Object>> pageList = page.getResult();
        long total = page.getTotal();
        Map<String, Object> m = new HashMap<>();
        m.put("pageList", pageList);
        m.put("total", total);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "UserId分页查询课程成功");
        map.put("dataobject", m);
        return map;
    }

    //Post请求   http://localhost:8089/course/updateCourse
    @PostMapping("/user/course/updateCourse")
    public Map<String, Object> updateCourse(@RequestBody Course course,
                                            @RequestHeader("userId") Integer userId) {
        Map<String, Object> tCourse = courseService.selectCourseById(course.getCourseId());
        Map<String, Object> map = new HashMap<>();
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "修改课程信息失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (courseService.updateCourse(course)) {
                map.put("code", 200);
                map.put("msg", "修改课程信息成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "修改课程信息失败，该Id不存在");
                map.put("dataobject", null);
            }
        }
        return map;
    }

    //Post请求   http://localhost:8089/course/updateScore
    @GetMapping("/course/updateScore/{courseId}")
    public Map<String, Object> updateScore(@PathVariable Integer courseId) {
        Map<String, Object> map = new HashMap<>();
        if (courseService.updateScore(courseId)) {
            map.put("code", 200);
            map.put("msg", "更新课程评分成功");
            map.put("dataobject", null);
        } else {
            map.put("code", 500);
            map.put("msg", "更新课程评分失败，该Id不存在");
            map.put("dataobject", null);
        }
        return map;
    }

    //Get请求   http://localhost:8089/course/addAttendNum/{courseId}
    @GetMapping("/course/addAttendNum/{courseId}")
    public Map<String, Object> addAttendNum(@PathVariable Integer courseId) {
        Map<String, Object> map = new HashMap<>();
        if (courseService.addAttendNum(courseId)) {
            map.put("code", 200);
            map.put("msg", "增加课程参与人数成功");
            map.put("dataobject", null);
        } else {
            map.put("code", 500);
            map.put("msg", "增加课程参与人数失败，该Id不存在");
            map.put("dataobject", null);
        }
        return map;
    }

    //Get请求   http://localhost:8089/course/deleteCourse/{courseId}
    @GetMapping("/user/course/deleteCourse/{courseId}")
    public Map<String, Object> deleteCourse(@PathVariable Integer courseId,
                                            @RequestHeader("userId") Integer userId) {
        Map<String, Object> tCourse = courseService.selectCourseById(courseId);
        Map<String, Object> map = new HashMap<>();
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "删除课程失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (courseService.deleteCourse(courseId)) {
                map.put("code", 200);
                map.put("msg", "删除课程成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "删除课程失败，该Id不存在");
                map.put("dataobject", null);
            }
        }
        return map;
    }
}
